// 车位费用设置模拟数据
const Mock = require('mockjs')

// 生成费用记录数据
const fees = [
  {
    id: 1,
    carType: "小型车",
    money: 5,
    moneyDesc: "白天标准收费",
    startTime: "08:00",
    endTime: "18:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 2,
    carType: "小型车",
    money: 3,
    moneyDesc: "夜间优惠收费",
    startTime: "18:00",
    endTime: "08:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 3,
    carType: "中型车",
    money: 8,
    moneyDesc: "白天标准收费",
    startTime: "08:00",
    endTime: "18:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 4,
    carType: "中型车",
    money: 5,
    moneyDesc: "夜间优惠收费",
    startTime: "18:00",
    endTime: "08:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 5,
    carType: "大型车",
    money: 12,
    moneyDesc: "白天标准收费",
    startTime: "08:00",
    endTime: "18:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 6,
    carType: "大型车",
    money: 8,
    moneyDesc: "夜间优惠收费",
    startTime: "18:00",
    endTime: "08:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 7,
    carType: "新能源车",
    money: 4,
    moneyDesc: "白天优惠收费",
    startTime: "08:00",
    endTime: "18:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  },
  {
    id: 8,
    carType: "新能源车",
    money: 2,
    moneyDesc: "夜间优惠收费",
    startTime: "18:00",
    endTime: "08:00",
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
]

// 获取费用列表
Mock.mock(/\/fee\/list(\?.*)?$/, 'get', (options) => {
  const url = options.url
  const carType = url.match(/carType=([^&]*)/)
  
  let result = [...fees]
  
  // 根据车型过滤
  if (carType && carType[1]) {
    result = result.filter(item => item.carType.includes(decodeURIComponent(carType[1])))
  }
  
  return {
    flag: true,
    code: 200,
    message: '获取成功',
    data: result
  }
})

// 添加费用设置
Mock.mock('/fee/add', 'post', (options) => {
  const { body } = options
  let params = {}
  
  try {
    params = JSON.parse(body)
  } catch (e) {
    return {
      flag: false,
      code: 400,
      message: '参数错误'
    }
  }
  
  // 验证必填字段
  if (!params.carType || !params.money || !params.startTime || !params.endTime) {
    return {
      flag: false,
      code: 400,
      message: '请填写完整的费用信息'
    }
  }
  
  // 生成新的费用记录
  const newFee = {
    id: fees.length + 1,
    carType: params.carType,
    money: params.money,
    moneyDesc: params.moneyDesc || '',
    startTime: params.startTime,
    endTime: params.endTime,
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  // 添加到记录中
  fees.push(newFee)
  
  return {
    flag: true,
    code: 200,
    message: '添加成功',
    data: newFee
  }
})

// 更新费用设置
Mock.mock('/fee/update', 'post', (options) => {
  const { body } = options
  let params = {}
  
  try {
    params = JSON.parse(body)
  } catch (e) {
    return {
      flag: false,
      code: 400,
      message: '参数错误'
    }
  }
  
  // 验证必填字段
  if (!params.id || !params.carType || params.money === undefined || !params.startTime || !params.endTime) {
    return {
      flag: false,
      code: 400,
      message: '请填写完整的费用信息'
    }
  }
  
  // 查找要更新的费用记录
  const index = fees.findIndex(item => item.id === params.id)
  
  if (index === -1) {
    return {
      flag: false,
      code: 404,
      message: '未找到该费用记录'
    }
  }
  
  // 更新记录
  fees[index] = {
    ...fees[index],
    carType: params.carType,
    money: params.money,
    moneyDesc: params.moneyDesc || fees[index].moneyDesc,
    startTime: params.startTime,
    endTime: params.endTime,
    feeTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  return {
    flag: true,
    code: 200,
    message: '修改成功',
    data: {
      flag: true
    }
  }
})

// 删除费用设置
Mock.mock(/\/fee\/delete\?id=\d+/, 'get', (options) => {
  const url = options.url
  const id = parseInt(url.split('=')[1])
  
  if (!id) {
    return {
      flag: false,
      code: 400,
      message: '参数错误'
    }
  }
  
  // 查找要删除的费用记录
  const index = fees.findIndex(item => item.id === id)
  
  if (index === -1) {
    return {
      flag: false,
      code: 404,
      message: '未找到该费用记录'
    }
  }
  
  // 删除记录
  fees.splice(index, 1)
  
  return {
    flag: true,
    code: 200,
    message: '删除成功',
    data: {
      flag: true
    }
  }
})

export default {
  fees
} 